package com.base.look.presenter;

import android.app.Activity;
import android.util.Log;

import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.CallBack;
import com.base.commonlib.http.CallBackFroPark;
import com.base.commonlib.http.FileBean;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.ResponseT;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.BitmapUtil;
import com.base.look.bean.AddLookBean;
import com.base.look.utils.MapTrunPojo;
import com.base.look.view.EditLookView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

public class EditLookPresenter extends BasePresenter<EditLookView> {
    public EditLookPresenter(EditLookView view, Activity activity) {
        super(view, activity);
    }


    public void addLook(String content){
        Map<String, String> map = new HashMap<>();
        map.put("content",content);
        map.put("array","");
        Disposable enqueue = RealNetWorkUtil.postForm().method("api-view/publish")
                .params(map)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.showError();
                        mView.returnAddLookError();
                    }

                    @Override
                    public void onNext(String s) {
                        mView.returnAddLookSuccess(s);
                    }
                });
        mCompositeDisposable.add(enqueue);
    }





    public void addLook2(List<String> list,AddLookBean addLookBean){
        Map<String,Object>maps= MapTrunPojo.object2Map(addLookBean);
        mView.showPb_mes("正在上传。。。");
        List<String> paths = new ArrayList<>();
        if (list.toString().contains("mp4")){
            for (String fils : list){
                paths.add(fils);
            }
        }else {
            for (String fils : list){
                paths.add(BitmapUtil.compressImage(fils));
            }
        }

        //压缩完成
        List<FileBean> mFileList = new ArrayList<>();
        FileBean mFileBean = null;
        for (String str : paths){
            mFileBean = new FileBean("files",str);
            mFileList.add(mFileBean);
        }
        Disposable enqueue = RealNetWorkUtil.postPics().method("api-view/publish").filePaths(mFileList)
                .params(maps)
                .build().enqueueForUpFiles(new CallBackFroPark<ResponseT>() {
                    @Override
                    public void onResponse(ResponseT responseT) throws Exception {
                        mView.hidePb_mes();
                        // mHasSaved = true;
                        mView.returnAddLookSuccess(responseT.getMsg());
                       // mView.showMes(responseT.getData().toString());
                    }

                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.returnAddLookError();
                    }

                    @Override
                    public void onNext(ResponseT responseT) {
                        if (responseT == null){
                            mView.hidePb_mes();
                            mView.returnAddLookSuccess(responseT.getMsg());
                        }
                    }
                });
        mCompositeDisposable.add(enqueue);
    }



    public void addLook3(List<String> list,AddLookBean addLookBean){
        Map<String,Object>maps= MapTrunPojo.object2Map(addLookBean);
        mView.showPb_mes("正在压缩图片。。。");
        List<String> paths = new ArrayList<>();
        for (String fils : list){
            paths.add(fils);
        }
        //压缩完成
        List<FileBean> mFileList = new ArrayList<>();
        FileBean mFileBean = null;
        for (String str : paths){
            mFileBean = new FileBean("files",str);
            mFileList.add(mFileBean);
        }
        Disposable enqueue = RealNetWorkUtil.postFile().method("api-view/publish").filePaths(mFileList)
                .params(maps)
                .build().enqueueForUpFiles(new CallBackFroPark<ResponseT>() {
                    @Override
                    public void onResponse(ResponseT responseT) throws Exception {
                        mView.hidePb_mes();
                        // mHasSaved = true;
                        mView.returnAddLookSuccess(responseT.getMsg());
                    }

                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.returnAddLookError();
                    }

                    @Override
                    public void onNext(ResponseT responseT) {
                        if (responseT == null){
                            mView.hidePb_mes();
                            mView.returnAddLookSuccess(responseT.getMsg());
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }



}
